﻿using System;
using UFIDA.U9.BS.SiteKeyProviders;
using UFIDA.U9.WSS.ProxyService.Models;
using UFIDA.UBF.SystemManage;
using UFSoft.UBF.Service;
using UFSoft.UBF.Util.Context;

namespace UFIDA.U9.WSS.ProxyService.Host
{
    /// <summary>
    ///     WSS PortalSiteKeyProvider
    ///     包装后注册服务
    /// </summary>
    public class WSSPortalSiteKeyProvider : IDirectoryProvicder, IAppInfoProvider, IServiceCheckProvider
    {
        public WSSPortalSiteKeyProvider(PortalSiteKeyProvider provider)
        {
            if (provider == null)
                throw new ArgumentException("PortalSiteKeyProvider is null");
            Provider = provider;
        }

        public PortalSiteKeyProvider Provider { get; }

        public void InitBPMap()
        {
            Provider.InitBPMap();
        }

        public void UpdateAppStateCache()
        {
            Provider.UpdateAppStateCache();
        }

        public SvcEndpoint GetAddress(string srvName, UFSoft.UBF.Service.Token token)
        {
            return Provider.GetAddress(srvName, token);
        }

        public bool IsServiceEnabled(string bpName, UFSoft.UBF.Service.Token token, out string errorMsg)
        {
            if (PlatformContext.Current == null ||
                PlatformContext.Current[SessionObject.PlatformContextExtend_DisableCheckOneProxyEnabled] == null ||
                PlatformContext.Current[SessionObject.PlatformContextExtend_DisableCheckOneProxyEnabled].ToString() != true.ToString())
                return Provider.IsServiceEnabled(bpName, token, out errorMsg);
            //只使用一次
            PlatformContext.Current[SessionObject.PlatformContextExtend_DisableCheckOneProxyEnabled] = null;
            errorMsg = string.Empty;
            return true;
        }

        public bool IsTargetOrgUserEnabled(long targetOrgID)
        {
            return Provider.IsTargetOrgUserEnabled(targetOrgID);
        }
    }
}